package cn.iocoder.yudao.module.pay.service.paycollectinfo;

import cn.iocoder.yudao.framework.common.enums.pay.PayCollectTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pay.controller.admin.collect.vo.PayCollectInfoPageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.collect.vo.PayCollectInfoSaveReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.paycollectinfo.PayCollectInfoDO;
import cn.iocoder.yudao.module.pay.dal.mysql.paycollectinfo.PayCollectInfoMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.COLLECT_INFO_MISSING;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.COLLECT_INFO_NOT_EXISTS;

/**
 * 收款账户信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PayCollectInfoServiceImpl implements PayCollectInfoService {

    @Resource
    private PayCollectInfoMapper collectInfoMapper;

    @Override
    public Long createCollectInfo(PayCollectInfoSaveReqVO createReqVO) {
        checkData(createReqVO);
        // 插入
        PayCollectInfoDO collectInfo = BeanUtils.toBean(createReqVO, PayCollectInfoDO.class);
        collectInfoMapper.insert(collectInfo);
        // 返回
        return collectInfo.getId();
    }

    @Override
    public void updateCollectInfo(PayCollectInfoSaveReqVO updateReqVO) {
        checkData(updateReqVO);
        // 校验存在
        validateCollectInfoExists(updateReqVO.getId());
        // 更新
        PayCollectInfoDO updateObj = BeanUtils.toBean(updateReqVO, PayCollectInfoDO.class);
        collectInfoMapper.updateById(updateObj);
    }

    private static void checkData(PayCollectInfoSaveReqVO updateReqVO) {
        if (PayCollectTypeEnum.BANK.getType().equals(updateReqVO.getType())) {
            if (updateReqVO.getBankAddr() == null || updateReqVO.getBankName() == null) {
                throw exception(COLLECT_INFO_MISSING);
            }
        } else {
            if (updateReqVO.getAccountQrUrl() == null) {
                throw exception(COLLECT_INFO_MISSING);
            }
        }
    }

    @Override
    public void deleteCollectInfo(Long id) {
        // 校验存在
        validateCollectInfoExists(id);
        // 删除
        collectInfoMapper.deleteById(id);
    }

    private void validateCollectInfoExists(Long id) {
        if (collectInfoMapper.selectById(id) == null) {
            throw exception(COLLECT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public PayCollectInfoDO getCollectInfo(Long id) {
        return collectInfoMapper.selectById(id);
    }

    @Override
    public PageResult<PayCollectInfoDO> getCollectInfoPage(PayCollectInfoPageReqVO pageReqVO) {
        return collectInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PayCollectInfoDO> getCollectInfoInUse() {
        return collectInfoMapper.selectList(PayCollectInfoDO::getStatus, 1);
    }

}